package rexsee.activity;

import rexsee.core.application.RexseeConfiguration;
import rexsee.core.browser.Browser;
import rexsee.core.lang.RexseeLanguageInterface;
import rexsee.core.layout.Layout;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;

import com.baidu.mobstat.StatService;

public class RexseeActivity extends Activity {

	protected Layout layout;
	protected int orientation;

	//For override in subclass
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		orientation = getResources().getConfiguration().orientation;

		RexseeLanguageInterface.init(this);
		layout = new Layout(this);
		this.setContentView(layout);

		RexseeActivityInit.initBasicUrlListener(layout.browser);
		RexseeActivityInit.initContentUrlListener(layout.browser);
		RexseeActivityInit.initRiskyUrlListener(layout.browser);
		RexseeActivityInit.initPredefinedJavaScriptInterface(layout.browser);
		RexseeActivityInit.initDefaultJavaScriptInterface(layout);
		RexseeActivityInit.initLayoutJavaScriptInterface(layout);
		RexseeActivityInit.initRiskyJavaScriptInterface(layout);
		RexseeActivityInit.initServiceJavaScriptInterface(layout);
		RexseeActivityInit.initUserAgent(layout.browser);

		layout.browser.initIntent = getIntent();
		String url = getIntent().getDataString();
		if (url == null) url = layout.browser.application.getCurrentHome();
		if (!url.equals(layout.browser.getUrl())) layout.browser.function.load(url);

	}
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		layout.browser.initIntent = intent;
		String url = intent.getDataString();
		if (url != null && !url.equals(layout.browser.getUrl())) layout.browser.function.load(url);
	}

	//Can't override in subclass
	@Override
	protected final void onPause() {
		super.onPause();
		layout.browser.pauseListeners.run();
		if (layout.browser.application.resources.baiduStatistic) StatService.onPause(this);
	}
	@Override
	protected final void onResume() {
		super.onResume();
		layout.browser.resumeListeners.run();
		if (layout.browser.application.resources.baiduStatistic) StatService.onResume(this);
	}
	@Override
	protected final void onDestroy() {
		super.onDestroy();
		RexseeActivityInit.clearInterfaceMemory(layout.browser);
		layout.browser.onDestroy();
		System.gc();
	}
	@Override
	protected final void onActivityResult(int requestCode, int resultCode, Intent data) {
		layout.browser.activityResult.run(requestCode, resultCode, data);
	}
	@Override
	public final void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		if (orientation != newConfig.orientation) {
			orientation = newConfig.orientation;
			layout.browser.eventList.run(Browser.EVENT_ONSCREENORIENTATIONCHANGED, new String[]{RexseeConfiguration.getOrientation(orientation)});
		}
	}
	@Override
	public final boolean onPrepareOptionsMenu(Menu menu) {
		try {
			layout.browser.menu.composeOptionsMenu(menu);
		} catch (Exception e) {
		}
		return true;
	}
	@Override
	public final boolean onOptionsItemSelected(MenuItem item) {
		try {
			layout.browser.menu.clickOptionsMenu(item);
		} catch (Exception e) {
		}
		return false;
	}
	@Override
	public final void onOptionsMenuClosed(Menu menu) {
		try {
			layout.browser.menu.isOptionsMenuOpen = false;
		} catch (Exception e) {
		}
	}
	@Override
	public final boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK
				|| keyCode == KeyEvent.KEYCODE_MENU
				|| keyCode == KeyEvent.KEYCODE_SEARCH
				|| keyCode == KeyEvent.KEYCODE_FOCUS
				|| keyCode == KeyEvent.KEYCODE_VOLUME_UP
				|| keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
			return layout.browser.onKeyDown(keyCode, event);
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}
	@Override
	public final boolean onKeyUp(int keyCode, final KeyEvent event) {
		layout.browser.onKeyUp(keyCode, event);
		return true;
	}
	@Override
	public void onLowMemory() {
		super.onLowMemory();
		layout.browser.garbageCollector.freeMemory();
	}

}
